<?php
echo "<pre>";
/**
 * 
 * 根据用户标签抓取用户信息，填充用户搜索数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


ini_set("display_errors",ture);
error_reporting(E_ALL);

define("CRON_NAME","cron_get_user_search_by_user_tag");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
	
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		
		//获取tag的总数
		$count = $db_mblog_cron_topic_tag->cron_get_mblog_topic_count($this->start_id,array(1,2,3));
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
			$this->start_id = 0 ;
			$count = $db_mblog_cron_topic_tag->cron_get_mblog_topic_count($this->start_id,array(1,2,3,4));
		}
		
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		
		//分批次取tag名称
		$food_tag = $db_mblog_cron_topic_tag->cron_get_mblog_topic_list($this->start_id,$p,PCOUNT,array(1,2,3,4));
		
		foreach($food_tag as $value)
		{
			$max_time = "";
			
			$topic = $value["topic"];
			$id = $value["id"];
			$cron_start_time  = $value["cron_start_time"];
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			$black_word  = $value["black_word"];
			$show_type = $value["show_type"];
			if($topic)//处理抓取分页逻辑
			{
				$this->get_list_by_topic($topic,$id,1,$cron_start_time,$show_type,$black_word);
				
			}
	
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($topic,$id,$p=1,$starttime="",$show_type="",$black_word="")
	{
		global $config;
		
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		
		if($topic)
		{
			
			//根据话题抓取
			$user_list = $mblog_from_api->api_get_user_by_search(
				$q="",
				$nick="",
				$qq="", 
				$tags=$topic,
				$province="",
				$city="",
				$gender="",
				$isv="",
				$ip="",
				$sbirth="",
				$ebirth="",
				$comp="",
				$scho="",
				$sort="",
				$p,
				$count=50
			
				);

			
		//	var_dump($user_list);
		//	exit;
			
			echo "{$topic}:count:{$user_list["data"]["count"]}|get_count".count($user_list["data"]["result"]).":p:{$p}<br>\n";	
			
		
			if($user_list["data"]["result"])
			{
				foreach($user_list["data"]["result"] as $value1)
				{
					
					
					//用户信息入库
					$mblog_info = $value1;
					$db_user_info = new db_user_info();
					$sex = $mblog_info["gender"];
					$user_nickname = $mblog_info["name"];
					$province = $mblog_info["province"];
					$location =  $mblog_info["location"];
					$city = $mblog_info["city"];
					$company ="";
					$school= "";
					$uid = $mblog_info["id"];
					$verified_type = $mblog_info["verified_type"];
					$birthday ="";
					$desc =  $mblog_info["description"];
					
					if(!$uid)
					{
						continue;
					}
					
					@$db_user_info->cron_insert_user_info($uid,$sex,$user_nickname,$province,$city,$company,$school,$verified_type,$bithday,$desc);
					
					
					$db_user_search = new db_user_search();
					
					$from_type=$config["user_from_type"]["user_tag"];
					$ret = @$db_user_search->insert_user_search($uid,$sex,$province ,$city,$location,$birthday,$id,0,$user_nickname,$verified_type,$from_type);
						
					//抓取这个用户tag的列表
					$this->get_mblog_by_uid($uid,$id,$topic,$show_type);
					
					
					
		
					
				}
		
			}
				
		}
	
		if($p == 1)//当为第一页时，检查是否有多页需要抓取
		{	//最大页数为20，接口限制
			$total_page = ceil($user_list["data"]["count"]/50);
			$total_page = $total_page > 10 ? 10 : $total_page;
			echo "topic_page_count:{$total_page}<br>\n";
			
			if($max_time)
			{
				$sql ="update mblog_cron_topic_tag  set cron_start_time = {$max_time} where id ={$id}";
				$db_base->query($sql);
			}
			
			
			for($i = 2 ; $i <= $total_page;$i++)
			{
				$this->get_list_by_topic($topic,$id,$i,$starttime,$show_type,$black_word);
			}
			
		
			
			
		}
			
	}
	
	
	//搜用户的微薄
	function get_mblog_by_uid($uid,$id,$topic,$show_type,$page=1)
	{
		echo "get_mblog_by_uid-page:".$page."topic : $topic \n ";
		global $config;
		$start_id= 0;
		$mblog_from_api = new mblog_from_api();
		$db_mblog_pic_content = new db_mblog_pic_content();
		$db_user_search = new db_user_search();
		$db_user_info = new db_user_info();
		if($page == 1)
		{
			$user_search_item = $db_user_search->cron_get_user_search_item_by_uid_and_topic_id($uid,$id);
			$start_id = $user_search_item["start_id"];
		}
		
		echo "start_id:".$start_id."<br\n>";
		$start = ($page - 1) * 20;
		$mblog_list = $mblog_from_api->api_get_mblog_by_uids_and_search(
			$topic,
			$uid,
			$start,
			$num=20,
			$isred="",
			$zone="",
			$starttime="",
			$endtime="",
			$hasori=1,
			$hasret="",
			$hastext="",
			$haspic=1,
			$hasvideo="",
			$hasmusic="",
			$haslink="",
			$hasat="",
			$istag="",
			$nofilter="",
			$onlymid=""
		);
		
		$mids = array();
		$total_count = $mblog_list["data"]["count"];
		foreach( $mblog_list["data"]["result"] as $key => $value)
		{
			$mids[]  = $value["mid62"];
		}
		$mids = join(",",$mids);
		
		
		if(!$mids)
		{
			return false;
		}
		$mblog_list = $mblog_from_api->api_get_mblog_by_mids($mids,$trim_user=0);
		
		
		//入库处理 开始
		
		if(!$mblog_list["data"]["result"])
		{
			return false;
		}
		
		if($mblog_list["data"]["result"])
		{
				
					
				foreach($mblog_list["data"]["result"] as $value1)
				{
					$mid = $value1["mid"] ;
					
					echo "id_conpare : $mid -- $start_id"."\n";
					if($mid <= $start_id)//如果小于start_id当做处理过
					{
							continue;
					}
					
					
					$check_source = preg_replace("/<.*?>/","",$value1["source"]);
					
					echo $check_source."\n";
					$check_res = false;
					foreach($config["check_mblog_cron_source"] as $value)
					{
						if(stripos($check_source,$value) !== false)
						{
							
							$check_res = true;
							break;
						}
					}
					
					$black_word = unserialize($black_word);
					//检查黑名单的方法
					if($black_word)
					{
						$checked = true;
						foreach($black_word as $value) 
						{
							if(stripos($value1["text"],$value) !== false)
							{
								$checked = false;
								break;
							}
						}
						if($checked == false)
						{
							continue;
						}
					}
					
					if($check_res == true)
					{
						
						$u_time = strtotime($value1["created_at"]);
						if($max_time < $u_time) $max_time = $u_time;
						$create_time = date("Y-m-d H:i:s",$u_time);
						
						
						//获取微博坐标信息
						/*$coordinates = "";
						if($value1["geo"]["coordinates"])
						{
							$coordinates = $value1["geo"]["coordinates"][1].",".$value1["geo"]["coordinates"][0];
						}
						//获取坐标对应地址
						if($coordinates)
						{
							 $address_info = $mblog_from_api->api_geo_to_address($coordinates);
							if($address_info["data"]["result"])
							{
								$location = $address_info["data"]["result"][0]["province_name"]." ".$address_info["data"]["result"][0]["city_name"]." ".$address_info["data"]["result"][0]["address"];
							} 
						}
						*/
						echo "image:{$value1["bmiddle_pic"]}\n";
						
						$res = get_image::get_image_size_by_url($value1["bmiddle_pic"]);
						
						$srcW = $res["width"]; 
						$srcH = $res["height"]; 
						if($srcW < 440)
						{
							continue;
						}
						$pic_width_pubu = PUBU_WIDTH;
						$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
						if($pic_height_pubu < 150)
						{
							continue;
						}
						$value1["text"] = string::un_html($value1["text"]);
						$value1["text"] = mysql_escape_string($value1["text"]);
						$date =  date("Y-m-d",strtotime($value1["created_at"]));
						$reposts_count = $value1["reposts_count"] ;
						$last_insert_id = $db_mblog_pic_content->cron_insert_mblog_pic_content($value1["text"],$id,$topic,$value1["bmiddle_pic"],$pic_width_pubu,$pic_height_pubu,$value1["source"],$show_type,$from=1,$value1["user"]["id"],$value1["user"]["screen_name"],$date,$mid,$create_time,$reposts_count);
						
						if($last_insert_id)
						{
							//如果插入成功，更新用户搜索表和用户表的微博个数
							$db_user_search->update_user_search_by_uid($value1["user"]["id"],$id,1);
							$db_user_info->update_user_count_by_uid($value1["user"]["id"],"","",1);
							
						}
					
					}
				}
				
				
				
			
				if($page == 1)
				{
					
					$mblog_first = array_shift($mblog_list["data"]["result"]);
					$max_mid = $mblog_first["mid"];
				
					if($max_mid >  $start_id)
					{
						$db_user_search->cron_update_user_search_by_uid($uid,$id,$max_mid);
					}
					
					$total_page = ceil($total_count/20);
					
					if(!$start_id)
					{
						for($i= 2;$i <=$total_page ;$i++ )
						{
							$this->get_mblog_by_uid($uid,$id,$topic,$show_type,$i);
						}
						
					}
						
					
				}
				
		
			}
			////处理end
		
		
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
